package com.hongmeng.operater.listener.vodHandle;

import com.alibaba.druid.util.StringUtils;
import com.hongmeng.common.exception.BaseException;
import com.hongmeng.operater.annotation.VodCallbackHandlerAnnotation;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class VodBeanPostProcessor implements BeanPostProcessor {

  public static  Map<String,IVodCallbackHandler> vodCallbackPool = new HashMap<>();

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Nullable
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        VodCallbackHandlerAnnotation annotation =  bean.getClass().getAnnotation(VodCallbackHandlerAnnotation.class);
        if(annotation==null) {
            return bean;
        }
        //如果标记注解 且判断当前类实现了IVodCallbackHandler 采用策略模式 注入测试模式中
        if(bean instanceof IVodCallbackHandler) {

            String value = annotation.value();
            if(StringUtils.isEmpty(value)) {
                throw new BaseException("VodCallbackHandlerAnnotationz注解值不能为空");
            }
            vodCallbackPool.put(value,(IVodCallbackHandler)bean);
        }
        return bean;
    }
}
